package com.huika.dealontop.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;

import com.huika.dealontop.R;
import com.huika.dealontop.support.http.rsa.Base64Helper;
import com.huika.dealontop.views.MMAlert;
import com.huika.dealontop.views.ToastMsg;

import java.io.UnsupportedEncodingException;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.line.Line;
import cn.sharesdk.linkedin.LinkedIn;
import cn.sharesdk.onekeyshare.OnekeyShareTool;
import cn.sharesdk.onekeyshare.ShareInfo;
import cn.sharesdk.system.text.ShortMessage;
import cn.sharesdk.twitter.Twitter;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/11/30 18:37 <br/>
 * Description: TODO
 */
public class SharpHelper implements View.OnClickListener {

    private Dialog mSharpDialog;
    private Context mContext;
    private String mTitle;
    private String mContent;
    private String mComment; //LinkedIn 使用
    private String mImgUrl;
    private String mUrl;

    public void setOnClickSharpIconListener(OnClickSharpIconListener onClickSharpIconListener) {
        this.onClickSharpIconListener = onClickSharpIconListener;
    }

    private OnClickSharpIconListener onClickSharpIconListener;

    public SharpHelper(Context context,String title, String url, String content, String imgUrl){
        mContext = context;
        mTitle = title;
        mUrl = url;
        mContent = content;
        mImgUrl = imgUrl;
        mSharpDialog = MMAlert.createShowAlert(context, R.layout.alert_sharp_layout);
        initView();
    }

    private void initView() {
        mSharpDialog.findViewById(R.id.iv_sharp_copy).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.iv_sharp_cut).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.iv_sharp_facebook).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.iv_sharp_linkedin).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.iv_sharp_twitter).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.iv_sharp_line).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.iv_sharp_sms).setOnClickListener(this);
        mSharpDialog.findViewById(R.id.cancel).setOnClickListener(this);
    }

    public void show(){
        mSharpDialog.show();
    }

    public void dismiss(){
        mSharpDialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        dismiss();
        if(onClickSharpIconListener != null){
            onClickSharpIconListener.onClickSharp(view);
        }
        String platform = null;
        switch (view.getId()){
            case R.id.iv_sharp_copy:
                //复制
                ClipboardHelper.copy(mContent, UIUtils.getContext());
                ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.copy_success));
                break;
            case R.id.iv_sharp_cut:
                //截图
                break;
            case R.id.iv_sharp_facebook:
                platform = Facebook.NAME;
                //mContent = mContent;
                mUrl = null;
                mTitle = null;
                break;
            case R.id.iv_sharp_linkedin:
                platform = LinkedIn.NAME;
                if(mUrl != null && mUrl.startsWith("http://")){
                    mUrl = mUrl.substring(7);
                    mUrl= mUrl.replaceAll("&", "&amp;");
                }

                if(mContent != null){
                    mContent = mContent.replaceAll("&", "&amp;");
                }
                mComment = mContent;
                break;
            case R.id.iv_sharp_twitter:
                platform = Twitter.NAME;
                String time = String.valueOf(SystemClock.currentThreadTimeMillis());
                try {
                    /* *
                     *如果mContent 中含有 url,这个url 必须是一个外网url,并且能访问的
                     */
                    mContent = mContent + "&t=" + Base64Helper.encode(time.getBytes("utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                mUrl = null;
                mTitle = null;
                break;
            case R.id.iv_sharp_line:
                platform = Line.NAME;
                mUrl = null; //line会打这个链接拼接到内容里 要去掉
                mTitle = null;
                break;
            case R.id.iv_sharp_sms:
                platform = ShortMessage.NAME;
                mUrl = null;
                mTitle = null; //有标题会当作彩信发送
                break;
        }

        if(TextUtils.isEmpty(platform)) return;

        ShareInfo shareInfo = new ShareInfo();
        shareInfo.title = mTitle;
        shareInfo.titleUrl = mUrl; /* 商品的网页地址 */
        //shareInfo.imageUrl = mImgUrl;
        shareInfo.content = mContent; /* 分享内容 */
        //shareInfo.contentUrl = mTitleUrl;/* 商品的网页地址 */
        shareInfo.comment = mComment;
        shareInfo.url = mUrl;
        shareInfo.notificationIcon = R.mipmap.ic_launcher;
        shareInfo.notificationStr = UIUtils.getString(R.string.app_name);
        shareInfo.siteUrl = mUrl;
        shareInfo.silent = true;
        shareInfo.platform = platform;

        OnekeyShareTool.initShareInfo(mContext, shareInfo, null);
    }

    private Handler mSharpHandle = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg != null){
                switch (msg.what){
                    case OnekeyShareTool.SHARE_SUCCESS:

                        break;
                    case OnekeyShareTool.SHARE_FAIL:

                        break;
                    case OnekeyShareTool.SHARE_CANCEL:

                        break;
                }
            }
            super.handleMessage(msg);
        }
    };

    public interface OnClickSharpIconListener{
        void onClickSharp(View view);
    }
}
